const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");
//派单中的经办人
router.post('/receipt/pdsales', async ctx => {
    let user = ctx.user
    let tenantId = user.tenantId;
    let state = ctx.request.body.state
    let list = await db.query(`select distinct doId,doName from receipt_pd where 
    tenantId=${tenantId} and state='${state}' `,{type: QueryTypes.SELECT})
    return ctx.suc('',list)
})
// 派单列表
router.post('/receipt/pdlist', async ctx => {
    let user = ctx.user
    let tenantId = user.tenantId;
    let doId = ctx.request.body.doId;
    if(!doId) return ctx.err('请选择经办人')
    let state = ctx.request.body.state
    let sql = `select * from receipt_pd where tenantId=${tenantId} and state='${state}' and doId=${doId} order by buName,createdAt `
    let list = await db.query(sql,{type: QueryTypes.SELECT})
    list.map(row => {
        row.nowCheck = row.arrearsReal
        row.yhAmount = ''
    })
    return ctx.suc('', {
        total: list.length,
        list
    })
})
// 在收款中 删除派单收款的业务票据
router.post('/receipt/pddel', async ctx => {
    let user = ctx.user
    let tenantId = user.tenantId;
    let id = ctx.request.body.id;
    if(!id) return ctx.err('请选要删除的行')
    await db.models['receiptPd'].destroy({
        where: {
            tenantId,
            id
        }
    })
    return ctx.suc('已删除')
})

module.exports = router;
